<%@ page import="java.nio.charset.StandardCharsets" %>
<%@ page import="wtc.stu.entity.StuEntity" %>
<%@ page import="wtc.stu.dao.StuDaoTeacher" %><%--
  Created by IntelliJ IDEA.
  User: 马可
  Date: 2022/10/30
  Time: 15:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>教师信息处理</title>
</head>
<body>
<%
    try {
%>
<%
    try {
%>
<%--增加教师信息功能--%>
<%
    //从form表单获取数据
    String tname = new String(request.getParameter("tname").getBytes("ISO8859_1"), StandardCharsets.UTF_8);
    String tremark = new String(request.getParameter("tremark").getBytes("ISO8859_1"), StandardCharsets.UTF_8);
    int tid = Integer.parseInt(request.getParameter("tid"));
    int xid = Integer.parseInt(request.getParameter("xid"));
    // 使用set方法将获取到的数据进行存储并封装
    StuEntity teacherAdd = new StuEntity();
    teacherAdd.setXid(xid);
    teacherAdd.setTid(tid);
    teacherAdd.setTname(tname);
    teacherAdd.setTremark(tremark);
    // 调用相应类的addStu方法将封装好的数据作为参数发送到数据库
    StuDaoTeacher daoTeacher = new StuDaoTeacher();
    daoTeacher.addStu(teacherAdd);
%>
<%--增加成功提示信息--%>
<script type="text/javascript">
    alert("增加成功！");
    // 跳转到指定页面
    window.location.href = "teacher.jsp";
</script>
<%
} catch (Exception e) {
%>
<%--修改教师信息功能--%>
<%
    //从form表单获取数据
    String newTname = new String(request.getParameter("newTname").getBytes("ISO8859_1"), StandardCharsets.UTF_8);
    String newTremark = new String(request.getParameter("newTremark").getBytes("ISO8859_1"), StandardCharsets.UTF_8);
    int newTid = Integer.parseInt(request.getParameter("newTid"));
    int newXid = Integer.parseInt(request.getParameter("newXid"));
    // 使用set方法将获取到的数据进行存储并封装
    StuEntity teacherUpdate = new StuEntity();
    teacherUpdate.setTid(newTid);
    teacherUpdate.setXid(newXid);
    teacherUpdate.setTname(newTname);
    teacherUpdate.setTremark(newTremark);
    // 调用相应类的addStu方法将封装好的数据作为参数发送到数据库
    StuDaoTeacher daoTeacherUpdate = new StuDaoTeacher();
    daoTeacherUpdate.updateStu(teacherUpdate);
%>
<%--增加成功提示信息--%>
<script type="text/javascript">
    alert("修改成功！");
    // 跳转到指定页面
    window.location.href = "teacher.jsp";
</script>
<%
    }
%>
<%
} catch (Exception e) {
%>
<%--删除教师信息功能--%>
<%
    //从form表单获取数据
    int delTid = Integer.parseInt(request.getParameter("delTid"));
    // 调用相应类的方法，并将获取到的数据作为参数
    StuDaoTeacher daoTeacherDel = new StuDaoTeacher();
    daoTeacherDel.delStu(delTid);
    // 再次通过form表单获取到的数据调用相应类的getOneById方法进行查询，用返回值来进行判断
    StuEntity pd = daoTeacherDel.getOneById(delTid);
    int c = pd.getTid();
    if (c == 0) {
%>
<%--增加成功提示信息--%>
<script type="text/javascript">
    alert("删除成功！");
    // 跳转到指定页面
    window.location.href = "teacher.jsp";
</script>
<%
} else {
%>
<%--增加成功提示信息--%>
<script type="text/javascript">
    alert("删除失败！存在外键关联或其他原因！");
    // 跳转到指定页面
    window.location.href = "teacher.jsp";
</script>
<%
    }
%>
<%
    }
%>
</body>
</html>
